package com.cap.dreamcircle.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.cap.dreamcircle.Consts.Constants;
import com.cap.dreamcircle.Utils.ToastUtils;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

/**
 * @author liukanshan
 */

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
    private IWXAPI api;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, Constants.WEIXIN_APPID, false);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp resp) {
        switch (resp.errCode) {
           case BaseResp.ErrCode.ERR_OK:
                if(resp!=null&& !TextUtils.isEmpty(resp.transaction)){
                    finish();
                    return;
                }
                else if ((resp!=null)&&"wechat_login_state".equals(((SendAuth.Resp) resp).state)) {
                    String code = ((SendAuth.Resp) resp).code;
                    Constants.WECHAT_LOGIN_CODE = code;
//                    Constants.WECHAT_LOGIN_CODE_BIND=code;
                    finish();
                    return;
                }
                else if((resp!=null)&&"wechat_bind_state".equals(((SendAuth.Resp) resp).state)){
                    String code = ((SendAuth.Resp) resp).code;
//                    Constants.WECHAT_LOGIN_CODE = code;
                    Constants.WECHAT_LOGIN_CODE_BIND=code;
                    finish();
                    return;
                }
                else{
                    finish();
                }
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                if((!TextUtils.isEmpty(resp.transaction))&&(resp.transaction.contains("share_app"))){
                    ToastUtils.showShortToast(this,"分享已经取消!");
                }
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                break;
            default:
                break;
        }
        finish();
    }
}